package com.riant.code;

public class ReverseTest {
    /**
     * 翻转字符串
     * 0-->1-->2-->3-->4-->5-->6
     * 0-->6-->5-->4-->3-->2-->1
     */
    public static void main(String[] args) {
        LNode head = new LNode();
        head.add(new LNode(1, null));
        head.add(new LNode(2, null));
        head.add(new LNode(3, null));
        head.add(new LNode(4, null));
        head.add(new LNode(5, null));
        head.add(new LNode(6, null));
        head.printValue();
        LNode reverseNode = reverse(head);
        reverseNode.printValue();
    }

    public static LNode reverse(LNode head) {
        if (head == null || head.next == null) {
            return null;
        }
        LNode pre = null;
        LNode cur = null;
        LNode next = null;
        cur = head.next;
        next = cur.next;
        // pre = null;
        while (null != cur.next) {
            //pre=cur;
            cur.next = pre;
            pre = cur;
            cur = next;
            next = cur.next;
        }
        cur.next = pre;
        head.next = cur;
        return head;
    }
}
